<template>
  <el-form ref="postForm" :model="postForm" :rules="rules">
    <div class="shopListSave">
      <el-row>
        <el-col :span="14">
          <!-- <el-form-item label="类型" prop="type" v-if="type !== 1">
            <el-select v-model="postForm.type" placeholder="类型" clearable class="filter-item">
              <el-option v-for="item in typeImgsJson" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item> -->
          <el-form-item label="标题" prop="name">
            <el-input v-model.trim="postForm.name" />
          </el-form-item>
          <el-form-item label="摘要" prop="viceName" v-if="type === 1">
            <el-input v-model.trim="postForm.viceName" />
          </el-form-item>
          <!-- <el-form-item label="编号">
            <el-input v-model.trim="postForm.identifier" />
          </el-form-item> -->
          <!-- <el-form-item label="浏览量">
            <el-input v-model.trim="postForm.num" />
          </el-form-item> -->
        </el-col>
        <el-col :span="10" v-if="type !== 1">
          <el-form-item label="图片:" prop="image" style="margin-left: 30px;">
            <Upload :default-image.sync="postForm.image" :mode="6" />
          </el-form-item>
        </el-col>
      </el-row>
      <template v-if="type === 1">
        <el-form-item prop="remark" style="margin-bottom: 30px;">
          <Tinymce ref="editor" v-model="postForm.remark" :height="400" />
        </el-form-item>
      </template>
      <el-form-item>
        <el-button type="primary" @click="submit" >确定</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload'
import { shopSave, shopGetById } from '@/api/shop'
import { typeJson, typeImgsJson } from '../../constant'
const defaultForm = {
  id: 0,
  identifier: '',
  image: '',
  name: '',
  num: '',
  remark: '',
  type: '2',
  typeText: '',
  viceName: ''
}
export default {
  name: 'ShopListSave',
  components: { Tinymce, Upload },
  props: {
    type: {
      default: 1,
      type: Number
    }
  },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      typeJson, typeImgsJson,
      rules: {
        image: [{ required: true, message: '请选择', trigger: 'blur' }],
        type: [{ required: true, message: '请选择', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        viceName: [{ required: true, message: '请输入', trigger: 'blur' }],
        remark: [{ required: true, message: '请输入图文详情', trigger: 'blur' }],
      },
      isEdit: false
    }
  },
  created() {
    this.postForm.type = this.type
    this.isEdit = this.$route.query && this.$route.query.id && Number(this.$route.query.id) > 0
    if (this.isEdit) {
      this.fetchData(this.$route.query.id)
    }
  },
  methods: {
    fetchData(id) {
      shopGetById({ id: id }).then(response => {
        this.postForm = response
      }).catch(err => {
        console.log(err)
      })
    },
    submit() {
      this.$refs['postForm'].validate((valid) => {
        if (valid) {
          let params = Object.assign({}, this.postForm)
          delete params.createTime
          if (this.postForm.type === 1) {
            delete params.image
          } else {
            delete params.remark
            delete params.viceName
          }
          shopSave(params, this.isEdit).then((res) => {
            // this.$notify({
            //   title: '成功',
            //   message: `${this.isEdit ? '编辑' : '添加'}成功`,
            //   type: 'success',
            //   duration: 2000
            // })

            this.$message({
              message: `${this.isEdit ? '编辑' : '添加'}成功`,
              type: 'success'
            });
            this.$router.go(-1);
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.shopListSave {
  padding: 40px 45px 20px 50px;
}
</style>